<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of insertSurat
 *
 * @author brlnt
 */
class Pencarian extends CI_Controller {

    public function Pencarian() {
        parent::__construct();
		if(!$this->session->userdata('islogin')) redirect('auth');
        $this->load->model('pencarian_model');
    }

    public function index() {
		$data['title'] = 'Form Pencarian Surat';
		$data['kategori_parent']= $this->kategori_array();
        $this->load->view('pencarian_view', $data);
    }

	public function search(){
		//$keyword =\
		$keyword['kode_surat'] = $_POST['kode-surat']; 
		$keyword['nama_instansi'] = $_POST['nama-instansi']; 
		$keyword['tanggal_kerjasama']=$_POST['tanggal-kerjasama'];
		$keyword['masa_berlaku']=$_POST['masa-berlaku'];
		$keyword['kerjasama_mengenai']=$_POST['kerjasama'];
		$keyword['nomersurat']=$_POST['nomer-surat'];
		$keyword['unit_terkait']=$_POST['unit-terkait'];
		$keyword['implementasi']=$_POST['implementasi']; 
		//$kode_surat = $_POST['kode-surat']; 
		//$data['hasil'] = $this->pencarian_model->getSuratByKodeSurat($keyword);
		if (empty($keyword['implementasi']) and empty($keyword['kode_surat']) and empty($keyword['nama_instansi']) 
		and empty($keyword['tanggal_kerjasama']) and empty($keyword['masa_berlaku']) and empty($keyword['kerjasama_mengenai']) and empty($keyword['nomersurat']) and empty($keyword['unit_terkait'])){
			$data['message'] = "Anda belum menginputkan data pencarian, mohon inputkan kembali";
		} else {
			$query = $this->pencarian_model->selectAll($keyword);
			if ($query->num_rows() > 0){
				$data['hasil'] = $query;
			}else{
				$data['message'] = "Data tidak ditemukan";
			} 
        }
		$data['title'] = 'Hasil Pencarian Surat';
		$this->load->view('hasil_pencarian', $data); 
	}
	
	private function kategori_array(){
		$this->load->model('surat_model');
		$query = $this->surat_model->getCategoriesByParent(0);
		$arraydata = array();
		if ($query->num_rows() > 0) {
			foreach($query->result() as $row) {
				$hasilquery = array(
					'id' => $row->idkategori,
					'nama' => $row->nama_kategori
				);
				array_push($arraydata, $hasilquery);
			}
		} else{
			$hasilquery = array(
				'id' => "-2",
				'nama' => "Tidak ada data"
			);
			array_push($arraydata, $hasilquery);
		}
		
		return $arraydata;
	}
}
?>
